package com.gome.ocean.service.utils.job.writer;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gome.framework.AppContext;
import com.gome.framework.Env;
import com.gome.ocean.common.constants.Constants;
import com.gome.ocean.service.utils.DataXCommonUtils;
import com.gome.ocean.service.utils.job.CreateJobUtils;
import com.google.common.collect.Maps;

/**
 * Created by liuqingxia on 2016/7/1.
 */
public class RedisWriter {

    private static final Logger  LOG              = LoggerFactory.getLogger(RedisWriter.class);

    public static final String[] MAP_KEYS         = new String[] { "env", "buinessKey", "dataStructure", "keys",
                                                                   "values", "keySeparator", "oextendField", "encoding",
                                                                   "vdataStructure", "expire" };
    public static final String[] CAN_BE_NULL_KEYS = new String[] { "oextendField", "encoding", "vdataStructure" };

    public static Map<String, Object> getWriterMap(HttpServletRequest req) {
        Map<String, Object> writerMaps = Maps.newHashMap();
        for (String key : MAP_KEYS) {
            String paramName = CreateJobUtils.addPrefix(Constants.REQUEST_WRITER_PARAM_PRE, key);
            String paramValue = req.getParameter(paramName);
            if ("env".equals(key)) {
                Env env = AppContext.getEnv();
                String zkUrl = env.get("zookeeperUrl");
                writerMaps.put("zookeeperUrl", zkUrl);
                continue;
            }
            if ("expire".equals(key)) {
                String expireTime = req.getParameter(paramName);
                /*if (!expireTime.equals("-1")) {
                    expireTime = expireTime.concat("000");
                }*/          
                writerMaps.put("expire", expireTime);
                continue;
            }
            if (StringUtils.isBlank(paramValue)) {
                if (DataXCommonUtils.keyCanBeNull(key, CAN_BE_NULL_KEYS, req)) {
                    continue;
                } else {
                    throw new IllegalArgumentException("参数" + paramName + "不能为空");
                }
            }
            writerMaps.put(key, paramValue);
        }
        return writerMaps;
    }

}
